/**
 * Created by Administrator on 2017/11/7.
 * Copyright 2017-2018 汉威.智慧环保事业部 Limited.
 * All rights reserved.
 */
package com.hw.one.collect.radar.dao;

import com.hw.one.collect.radar.po.MultipleDataPo;
import com.hw.one.collect.radar.po.SingleDataPo;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;

@Repository(value = "RadarDataDao")
public class RadarDataDao {

    @Resource
    private MongoTemplate mongoTemplateStandardSiteData;

    private static String singleTableName = "T_DATA_DEVICE_RADAR_SINGLE";
    private static String multipleTableName = "T_DATA_DEVICE_RADAR_MULTIPLE";


    /**
     * 添加单条雷达数据
     *
     * @param singleDataPo
     */
    public void saveSingleRadarData(SingleDataPo singleDataPo) {
        mongoTemplateStandardSiteData.insert(singleDataPo, singleTableName + "-" + singleDataPo.getMONITORTIME().substring(0, 4));
    }

    /**
     * 删除单条雷达数据
     *
     * @param singleDataPo
     */

    public void deleteSingleRadarData(SingleDataPo singleDataPo) {
        Criteria criteria = Criteria.where("SITECODE").is(singleDataPo.getSITECODE());
        criteria.and("MONITORTIME").is(singleDataPo.getMONITORTIME());
        criteria.and("DATATYPE").is(singleDataPo.getDATATYPE());
        Query query = new Query(criteria);
        mongoTemplateStandardSiteData.remove(query, singleTableName + "-" + singleDataPo.getMONITORTIME().substring(0, 4));
    }

    /**
     * 查询单条数据 (最新的一条数据)
     *
     * @param singleDataPo
     * @return
     */

    public SingleDataPo findOneSingleDataBySiteCodeAndTime(SingleDataPo singleDataPo) {
        Criteria criteria = Criteria.where("SITECODE").is(singleDataPo.getSITECODE());
        criteria.and("MONITORTIME").is(singleDataPo.getMONITORTIME());
        criteria.and("DATATYPE").is(singleDataPo.getDATATYPE());
        Query query = new Query(criteria).with(new Sort(Sort.Direction.DESC, "MOINTORTIME"));

        return mongoTemplateStandardSiteData.findOne(query, SingleDataPo.class,
            singleTableName + "-" + singleDataPo.getCREATETIME().substring(0, 4));
    }


    /**
     * 添加周期雷达数据
     *
     * @param multipleDataPo
     */
    public void saveMultipleRadarData(MultipleDataPo multipleDataPo) {
        mongoTemplateStandardSiteData.insert(multipleDataPo, multipleTableName + "-" + multipleDataPo.getCREATETIME().substring(0, 4));
    }

    /**
     * 删除雷达周期数据
     *
     * @param multipleDataPo
     */

    public void deleteMultipleRadarData(MultipleDataPo multipleDataPo) {
        Criteria criteria = Criteria.where("SITECODE").is(multipleDataPo.getSITECODE());
        criteria.and("STARTTIME").is(multipleDataPo.getSTARTTIME());
        criteria.and("ENDTIME").is(multipleDataPo.getENDTIME());
        criteria.and("DATATYPE").is(multipleDataPo.getDATATYPE());
        Query query = new Query(criteria);
        mongoTemplateStandardSiteData.remove(query, multipleTableName + "-" + multipleDataPo.getCREATETIME().substring(0, 4));
    }

    /**
     * 查询周期数据 (最新的一条数据)
     *
     * @param multipleDataPo
     * @return
     */

    public MultipleDataPo findOneMultipleDataBySiteCodeAndTime(MultipleDataPo multipleDataPo) {
        Criteria criteria = Criteria.where("SITECODE").is(multipleDataPo.getSITECODE());
        criteria.and("STARTTIME").is(multipleDataPo.getSTARTTIME());
        criteria.and("ENDTIME").is(multipleDataPo.getENDTIME());
        criteria.and("DATATYPE").is(multipleDataPo.getDATATYPE());
        Query query = new Query(criteria).with(new Sort(Sort.Direction.DESC, "STARTTIME"));

        return mongoTemplateStandardSiteData.findOne(query, MultipleDataPo.class,
            multipleTableName + "-" + multipleDataPo.getCREATETIME().substring(0, 4));
    }


}

